/*
 * CodeSaveCommand.java
 *
 * Created on 2006년 3월 29일 (수), 오후 4:11
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package com.qbooks.app;

import java.util.*;





import javax.servlet.*;
import javax.servlet.http.*;

import org.json.simple.JSONObject;

import com.zll.framework.model.HashBox;
import com.zll.framework.model.HashList;
import com.zll.framework.servlet.Command;
import com.zll.framework.util.StringUtil;
import com.qbooks.app.AppDAO;
import com.qbooks.common.CommonUtil;





/**
 *
 * @author eastar
 */
public class LoginCommand implements Command
{
	private final String D = "\"";

    public String execute(HttpServletRequest  request, HttpServletResponse response)
    throws Exception
    {
    	String os	= (String)request.getAttribute("os");

    	String id		= StringUtil.nvl(request.getParameter("id"));
    	String pw		= StringUtil.nvl(request.getParameter("pw"));
    	String mac		= StringUtil.nvl(request.getParameter("mac"));
    	String model	= StringUtil.nvl(request.getParameter("model"));

    	HashMap infos = new HashMap();
    	infos.put("member_id", id);
    	infos.put("mac", mac);
    	infos.put("model", model);
    	
    	
    	int result_code = 100;

    	AppDAO dao 		= new AppDAO();

		if (id.equals("")) {
			result_code = 201;	//유효하지 않은 아이디
		}
		else if (pw.equals("")) {
			result_code = 203;	//유효하지 않은 비밀번호
		}
		else if (mac.equals("")) {
			result_code = 205;	//유효하지 않은 Mac Addres
		}
		else {

			HashList list = dao.selectMember(infos);

			if (list.size() == 1) {
				if (!pw.equals(list.get("member_pw",0))) {
					result_code =  204;	//비밀번호 불일치
				}
				else {
					int mac_count = list.getInt("mac_count", 0);
					int max_mac = list.getInt("max_mac", 0);
					int mac_yn =  list.getInt("mac_yn", 0);
					if (mac_yn == 0) {
						if (mac_count >= max_mac) {
							result_code = 305;
						} else {
							//맥추가
							dao.insertMemberMac(infos);
						}
					}
				}
			}
			else {
				result_code = 202;	//회원정보가 존재하지 않습니다.
			}
		}

		HashList bookList = new HashList();

		if (result_code == 100) {
			bookList = dao.selectMemberBookList(infos);
		}

		if ("android".equals(os)) {
			JSONObject json = new JSONObject();
	    	json.put("if_code", "LGN");
	    	json.put("result_code", result_code);
	    	json.put("result_list", bookList);
	    	request.setAttribute("jsonData", json.toString());
		}
		else if ("ios".equals(os)) {
			StringBuffer jsonData = new StringBuffer();

			if (result_code == 100) {
				jsonData.append("[");
				for (int i = 0; i < bookList.size(); i++) {
					HashMap map = (HashMap)bookList.get(i);
					jsonData.append("[")
							.append(D).append(map.get("book_seq")).append(D)
							.append(", ").append(D).append(map.get("book_header")).append(D)
							.append(", ").append(D).append(map.get("book_image_url")).append(D)
							.append(", ").append(D).append("n").append(D)
							.append(", ").append(D).append("n").append(D)
							.append(", ").append(D).append("n").append(D)
							.append(", ").append(D).append("n").append(D)
							.append(", ").append(D).append("n").append(D)
							.append(", ").append(D).append("n").append(D)
							.append(", ").append(D).append("n").append(D)
							.append("]");
				}
				jsonData.append("]");
			}
			else {
				jsonData.append("{\"status\", \""+result_code+"\"}");
			}

			request.setAttribute("jsonData", jsonData.toString());
		}

		return "/common/json_view.jsp";

    }
}
